USE [NGroup_VTL_DB]
GO

/****** Object:  StoredProcedure [dbo].[AddNewNews]    Script Date: 5/28/2015 10:01:12 PM ******/
DROP PROCEDURE [dbo].[AddNewNews]
GO

/****** Object:  StoredProcedure [dbo].[AddNewNews]    Script Date: 5/28/2015 10:01:12 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Nguyen Vu Nam
-- Create date: 20131218
-- Description:	Add new news
-- =============================================
CREATE PROCEDURE [dbo].[AddNewNews]
	
	@TypeID					INT,
	@UniqueString			NVARCHAR(500),
	@MetaContent			NVARCHAR(1500) = NULL,
	@MetaDescription		NVARCHAR(1500) = NULL,
	@IsActive				BIT,
	@Title					NVARCHAR(500),
	@BriefContent			NVARCHAR(4000),
	@FullContent			NVARCHAR(MAX),
	@ImageSmallURL			NVARCHAR(500),

	@MetaContentEn			NVARCHAR(1500) = NULL,
	@MetaDescriptionEn		NVARCHAR(1500) = NULL,
	@TitleEn				NVARCHAR(500),
	@BriefContentEn			NVARCHAR(4000),
	@FullContentEn			NVARCHAR(MAX),
	@ImageSmallURLEn		NVARCHAR(500),

	@CreatedAt				DATETIME,
	@CreatedBy				NVARCHAR(50),
	@NewIdentity			INT = NULL OUTPUT

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @HuongDanThanhToan	INT = 5,
			@KhachHang			INT = 6,
			@SiteMap			INT = 7,
			@LienHe				INT = 8

	IF(@TypeID = @HuongDanThanhToan 
		OR @TypeID = @KhachHang 
		OR @TypeID = @SiteMap 
		OR @TypeID = @LienHe)
	BEGIN
		UPDATE [dbo].[News]
		   SET
			  [Title] = @Title			  
			  ,[MetaContent] = @MetaContent
			  ,[MetaDescription] = @MetaDescription
			  ,[BriefContent] = @BriefContent
			  ,[FullContent] = @FullContent
			  ,[ImageSmallURL] = @ImageSmallURL

			  ,[Title_En] = @TitleEn			  
			  ,[MetaContent_En] = @MetaContentEn
			  ,[MetaDescription_En] = @MetaDescriptionEn
			  ,[BriefContent_En] = @BriefContentEn
			  ,[FullContent_En] = @FullContentEn
			  ,[ImageSmallURL_En] = @ImageSmallURLEn

			  ,[IsActive] = 1
			  ,[LastUpdatedAt] = @CreatedAt
			  ,[LastUpdatedBy] = @CreatedBy
		 WHERE [UniqueString] = @UniqueString
				AND ([TypeID] = @HuongDanThanhToan 
						OR [TypeID] = @KhachHang 
						OR [TypeID] = @SiteMap 
						OR [TypeID] = @LienHe)

		 SELECT @NewIdentity = @@ROWCOUNT
	END
	ELSE
	BEGIN
		IF NOT EXISTS(
						SELECT * 
						FROM [dbo].[News] WITH (NOLOCK) 
						WHERE [UniqueString] = @UniqueString
							  AND [IsActive] <> 0)
		BEGIN

		INSERT INTO [dbo].[News]
			   ([TypeID]
			   ,[UniqueString]
			   ,[MetaContent]
			   ,[MetaContent_En]
			   ,[MetaDescription]
			   ,[MetaDescription_En]
			   ,[IsActive]
			   ,[Title]
			   ,[BriefContent]
			   ,[FullContent]
			   ,[ImageSmallURL]
			   ,[Title_En]
			   ,[BriefContent_En]
			   ,[FullContent_En]
			   ,[ImageSmallURL_En]
			   ,[CreatedAt]
			   ,[CreatedBy])
		 VALUES
			   (@TypeID
			   ,@UniqueString
			   ,@MetaContent
			   ,@MetaContentEn
			   ,@MetaDescription
			   ,@MetaDescriptionEn
			   ,@IsActive
			   ,@Title
			   ,@BriefContent
			   ,@FullContent
			   ,@ImageSmallURL

			   ,@TitleEn
			   ,@BriefContentEn
			   ,@FullContentEn
			   ,@ImageSmallURLEn

			   ,@CreatedAt
			   ,@CreatedBy)
		END

		SELECT @NewIdentity = @@IDENTITY
	END
	
END


GO


